/*
 * main.cpp
 *
 *  Created on: Jul 2, 2013
 *      Author: dsweeney
 */

#include <fstream>
#include "LoggerSingleton.h"
#include <string>
#include <iostream>
#include <queue>
#include <string.h>
#include "Node.h"
#include "Agent.h"
#include <time.h>
///////////////////////////////////////////////////////////////////////////////
//Globals
std::ifstream sowpods;
std::ifstream rackfile;
///////////////////////////////////////////////////////////////////////////////
//forward declarations
bool openFiles (char* pathSOWPOD, char* pathRACKFILE);
Node* createLibrary(void);
void fillRacks (std::queue<char*>* );
///////////////////////////////////////////////////////////////////////////////
int main (void)
{
	std::queue<char*> racks;
	openFiles("C:\\Users\\dsweeney\\svn\\ScrabbleAgent\\src\\SOWPODS_complete.txt",
				"C:\\Users\\dsweeney\\svn\\ScrabbleAgent\\src\\sample_rack_file.txt");

	Node* head;
	fillRacks(&racks);
	char* current;
	Agent* agent;
	clock_t time;

	while(!racks.empty())
	{
		time = clock();
		head = createLibrary();
		time = clock() - time;
		std::cout<<"time to build tree = "<<((float)time)/CLOCKS_PER_SEC<<std::endl;

		std::string bestString;
		uint bestScore = 0;

		current = racks.front();
		racks.pop();
		std::cout<<"RACK = "<<current<<std::endl;
		agent = new Agent(head, current);

		time = clock();
		agent->makePlay();
		time = clock() - time;
		std::cout<<"search took = "<<((float)time)/CLOCKS_PER_SEC<<std::endl<<std::endl;

		//break;
		delete head;
		delete agent;
		delete[] current;
		sowpods.close();
		sowpods.open("C:\\Users\\dsweeney\\svn\\ScrabbleAgent\\src\\SOWPODS_complete.txt", std::ifstream::in);
	}
	rackfile.close();

	std::cout<<"EXIT SUCCESS"<<std::endl;
	return 1;
}
///////////////////////////////////////////////////////////////////////////////
bool openFiles (char* pathSOWPOD, char* pathRACKFILE)
{
	sowpods.open(pathSOWPOD, std::ifstream::in);
	rackfile.open(pathRACKFILE, std::ifstream::in);

	return (sowpods.is_open() && rackfile.is_open());
}
///////////////////////////////////////////////////////////////////////////////
Node* createLibrary()
{
	char line [50];
	Node* head = new Node(' ');

	//uncomment code below to get average word score. btw its 14
	/*unsigned long long sum = 0;
	uint words = 0;*/


	//first loop grab from the the file a line at a time.
	while(sowpods.good())
	{
		sowpods.getline(line, 50);
		//Logger::instance()->write(__FILE__, __LINE__, line);

		Node* current = head;
		//loop through each line creating nodes as we go
		//could be recursive.....
		for(int i = 0; (i < 50) && (isalpha(line[i])); i++)
		{
			char workingChar = tolower(line[i]);

			current->addChild(workingChar);

			current = current->getChild(workingChar);
		}
		//set whole word bool
		current->wholeWord = current == head?
				false:
				true;

		//uncomment code below to get average word score

		/*std::string* s = new std::string();
		current->getString(s);

		if(s->length() <= 7)
		{
			int score = current->getScore();
			sum += s->length() == 7? (score+50): score;
			words++;
		}
		delete s;*/
	}
	//std::cout<<"average score = "<<(sum/words)<<std::endl;
	return head;
}
///////////////////////////////////////////////////////////////////////////////
void fillRacks (std::queue<char*>* queue)
{
	while(rackfile.good())
	{
		char temp [50];
		rackfile.getline(temp, 50);
		char* rack = new char[8];//7 characters + null terminator

		for(int i = 0; (i < 8) && (isalpha(temp[i]) || temp[i] == '_'); i++)
		{
			rack[i] = tolower(temp[i]);
		}
		rack[7] = '\0';
		if(strlen(rack) > 1)
			queue->push(rack);
	}
}
